<?php
namespace app\common\model;
class Social extends Base{
	/**
	 * 通过微信好友ID获取与该好友的聊天记录
	 * @param  array $where 条件
	 * @return Social
	 */
    function logs($where=[]){
    	$options=[
    		'with'=>['software','software.device'],
    		'field'=>'id,soft_id,name,number,head_img',
    	];
    	$list=$this->readAll($where,true,$options);
		$list=$list->toArray();
		// 做一个排序
		foreach($list['data'] as $k => $v) {
			$logs=model('Weixinlogs')->logs(['social_id'=>$v['id'],'soft_id'=>$v['soft_id']]);
			$v['total']=$logs['total'];
			$v['per_page']=$logs['per_page'];
			$v['current_page']=$logs['current_page'];
			$v['last_page']=$logs['last_page'];
			$v['weixinlogs']=$logs['data'];
			$list['data'][$k]=$v;
		}

    	$this->edit(['unread'=>0],array_merge($where,['unread'=>1])); //设置已读
    	return $list;
    }
    function weixinlogs(){
    	return $this->hasMany('weixinlogs');
    }
    function software(){
    	return $this->belongsTo('software','soft_id')->field('id,name,device_id');
    }
}
